#include "stdafx.h"
#include "FSMPlayerMe.h"
#include "PlayerMe.h"
#include "StateMove.h"
#include "StateIdleMe.h"
#include "StateMeAttack.h"

FSMPlayerMe::FSMPlayerMe(PlayerMe *obj)
{
	m_object = obj;
	auto smove = new StateMove(this, obj);
	smove->m_cb_position = std::bind(&FSMPlayerMe::cb_movePosition, this, smove, _1);
	m_states[FSM::st_idle] = new StateIdleMe(this, obj);
	m_states[FSM::st_move] = smove;
	auto attack = new StateMeAttack(this, obj);
	m_states[FSM::st_attack] = attack;
}


FSMPlayerMe::~FSMPlayerMe()
{
	m_object = nullptr;
}

bool FSMPlayerMe::cb_movePosition(StateMove * stateMove, POINT & cell)
{
	switch (stateMove->m_fsm->GetNextState())
	{
	case FSM::st_move:
		if (m_move.pt.x != stateMove->m_endPoint.x ||
			m_move.pt.y != stateMove->m_endPoint.y)
		{
			stateMove->MakePath(m_move.pt);
		}
		SetNextState(st_idle);
		return false;
	case FSM::st_attack:
	{
		int dist1 = m_object->GetSkillUseDistance(m_attack.attackType);
		int dist2 = m_object->CalcTargetDistance(m_attack.target);
		if (dist2 > (dist1 + m_object->GetRadius() + m_attack.target->GetRadius()))
		{
			FSM::Move move;
			move.dirIdx = -1;
			move.pt = ((LifeObject*)m_attack.target)->GetMapPos();
			if (move.pt.x != stateMove->m_endPoint.x ||
				move.pt.y != stateMove->m_endPoint.y)
			{
				stateMove->MakePath(m_move.pt);
			}
			return false;
		}
		else
		{
			stateMove->Exit();
			ChangeToNextState();
			return true;
		}
	}
	}
	return false;
}
